/** 
* Project Name:lg_service 
* File Name:AdminNativeChannelController.java 
* Package Name:com.wl.api.controller.system 
* Date:2018年5月7日下午11:19:41 
* Copyright (c) 2018, lanls All Rights Reserved. 
* 
*/
package com.wl.api.controller.system;

import java.util.Date;
import java.util.function.Predicate;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.wl.api.common.utils.ErrorCodeEnum;
import com.wl.api.common.utils.JsonObject;
import com.wl.api.controller.base.CommonController;
import com.wl.api.dto.base.CommonUserDTO;
import com.wl.api.dto.system.SecurityNativeChannelNetworkDTO;
import com.wl.api.model.system.SecurityNativeChannelNetwork;
import com.wl.api.service.system.ISecurityNativeChannelNetworkService;

/**
 * ClassName: AdminNativeChannelController <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2018年5月7日 下午11:19:41 <br/>
 * 
 * @author lanls
 * @version
 * @since JDK 1.6
 */
@RestController
@RequestMapping("admin/nativie/channel/network")
public class AdminNativeChannelNetworkController extends CommonController {

	@Autowired
	private ISecurityNativeChannelNetworkService nativeChannelNetworkService;

	/**
	 * 添加渠道网点
	 * 
	 * @param param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "manage", method = RequestMethod.POST)
	public JsonObject create(@RequestBody SecurityNativeChannelNetworkDTO param) throws Exception {
		JsonObject json = JsonObject.getInstance();
		if (validate().test(param)) {
			json.setMessage(ErrorCodeEnum.param_error.getMessage());
			json.setErrorCode(ErrorCodeEnum.param_error.getError_code());
			json.setStatus(JsonObject.STATUS_FAIL);
		} else {
			CommonUserDTO user = getCommonUser();
			SecurityNativeChannelNetwork nativeChannel = new SecurityNativeChannelNetwork();
			BeanUtils.copyProperties(nativeChannel, param);
			if (null == nativeChannel.getStatus()) {
				nativeChannel.setStatus(1);
			}
			nativeChannel.setUpdateTime(new Date());
			nativeChannel.setUpdateUser(user.getUsername());
			nativeChannelNetworkService.insert(nativeChannel);
			json.setData(nativeChannel.getId());
			json.setMessage("操作成功");
			json.setStatus(JsonObject.STATUS_SUCCESS);
		}
		return json;
	}

	/**
	 * 更新渠道网点
	 * 
	 * @param param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "manage", method = RequestMethod.PUT)
	public JsonObject modify(@RequestBody SecurityNativeChannelNetworkDTO param) throws Exception {
		JsonObject json = JsonObject.getInstance();
		if (validate().or(s -> null == param.getId()).test(param)) {
			json.setMessage(ErrorCodeEnum.param_error.getMessage());
			json.setErrorCode(ErrorCodeEnum.param_error.getError_code());
			json.setStatus(JsonObject.STATUS_FAIL);
		} else {
			CommonUserDTO user = getCommonUser();
			SecurityNativeChannelNetwork nativeChannel = new SecurityNativeChannelNetwork();
			BeanUtils.copyProperties(nativeChannel, param);
			nativeChannel.setUpdateTime(new Date());
			nativeChannel.setUpdateUser(user.getUsername());
			nativeChannelNetworkService.updateById(nativeChannel);
			json.setData(nativeChannel.getId());
			json.setMessage("操作成功");
			json.setStatus(JsonObject.STATUS_SUCCESS);
		}
		return json;
	}

	/**
	 * 更新状态
	 * 
	 * @param id
	 * @param status
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "manage/{id}/{status}", method = RequestMethod.PUT)
	public JsonObject modifyStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status)
			throws Exception {
		JsonObject json = JsonObject.getInstance();
		getCommonUser();
		nativeChannelNetworkService.updateById(SecurityNativeChannelNetwork.builder().status(status).id(id).build());
		setJsonMessage(json);
		return json;
	}

	/**
	 * 删除渠道网点
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/manage/{id}", method = RequestMethod.DELETE)
	public JsonObject delete(@PathVariable("id") Long id) throws Exception {
		JsonObject json = JsonObject.getInstance();
		getCommonUser();
		nativeChannelNetworkService.deleteById(id);
		setJsonMessage(json);
		return json;
	}

	/**
	 * 查询单个渠道网点
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/manage/{id}", method = RequestMethod.GET)
	public JsonObject getDetail(@PathVariable("id") Long id) throws Exception {
		getCommonUser();
		JsonObject json = JsonObject.getInstance();
		json.setData(nativeChannelNetworkService.selectById(id));
		setJsonMessage(json);
		return json;
	}

	/**
	 * 动态参数查询列表
	 * 
	 * @param param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/manage", method = RequestMethod.GET)
	public JsonObject getItemList(SecurityNativeChannelNetworkDTO param) throws Exception {
		JsonObject json = JsonObject.getInstance();
		getCommonUser();
		param.buildStartTime(param.getStartTime()).buildEndTime(param.getEndTime()).buildPage(param.getPage(),
				param.getPageSize());
		json.setPage(nativeChannelNetworkService.selectByDynamicParameter(param));
		setJsonMessage(json);
		return json;
	}

	private Predicate<SecurityNativeChannelNetworkDTO> validate() {
		return s -> StringUtils.isBlank(s.getName()) || null == s.getAreaId() || null == s.getChannelId()
				|| null == s.getAddress() || StringUtils.isBlank(s.getLogo());
	}
}
